# coding: utf-8
import requests
import pymysql
import re
from xpinyin import Pinyin
from bs4 import BeautifulSoup

hrefs = []
# 获取数据库连接
conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='admin',
                       database='weather', charset='utf8')
cursor = conn.cursor()

def getCitypinyin(str):
    pinyin = Pinyin()
    result = pinyin.get_pinyin(str, '')
    return result

def getHTMLText(url):
    try:
        headers = {"User-Agent": "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0; SE 2.X MetaSr 1.0; SE 2.X MetaSr 1.0; .NET CLR 2.0.50727; SE 2.X MetaSr 1.0)"}
        r = requests.get(url, timeout = 30, headers=headers)
        r.raise_for_status()
        r.encoding = r.apparent_encoding
        return r.text
    except:
        return ""


class getWeater(object):

    def __init__(self):
        self.city = '北京'
        self.citypinyin = getCitypinyin(self.city)
        self.url = "http://lishi.tianqi.com/" + self.citypinyin + '/index.html'
        self.headers = {
            'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/82.0.4083.0 Safari/537.36 Edg/82.0.458.0'
        }

    def getHistoryHref(self):
        repon = requests.get(self.url, headers=self.headers)
        repon.raise_for_status()
        repon.encoding = repon.apparent_encoding
        html_index = repon.text
        index_soup = BeautifulSoup(html_index, "html.parser")
        for hrefsList in index_soup.select('.clearfix'):
            for i in range(len(hrefsList.select('li > a'))):
                hrefs.append(hrefsList.select('li > a')[i]['href'])
        return hrefs

    def getHistoryWeather(self):
        urls = GlobalVar.weather.getHistoryHref()
        print(urls)
        fakesUrls = ['http://lishi.tianqi.com/beijing/202003.html', 'http://lishi.tianqi.com/beijing/202002.html']
        for url in urls:
            hmlText = getHTMLText(url)
            htmlSoup = BeautifulSoup(hmlText, 'html.parser')
            # print(htmlSoup)
            for weatherList in htmlSoup.select('.lishitable_content > li'):
                for i in range(len(weatherList.select('div'))):
                    date = weatherList.select('div')[0].text
                    highTemperature = weatherList.select('div')[1].text
                    lowTemperature = weatherList.select('div')[2].text
                    condition = weatherList.select('div')[3].text
                    wind = weatherList.select('div')[4].text
                    city = self.city
                print('城市：' + city +' 日期：' + date + ' 最高气温：' + highTemperature
                          + ' 最低气温：' + lowTemperature + ' 天气：' + condition + ' 风向：' + wind)
                cursor.execute(
                    "insert into forecast(city, weathers, date, HighTemperature, LowTemperature, wind) VALUES('{0}', '{1}', '{2}', '{3}', '{4}', '{5}')"
                        .format(city, condition, date, highTemperature, lowTemperature, wind))
                conn.commit()

class GlobalVar(object):
    weather = getWeater()

GlobalVar.weather.getHistoryWeather()
